#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define str string
#define all(x) (x).begin(),(x).end()
#define ms(a,t) memset(a,t,sizeof(a))
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MP make_pair
#define pb push_back
#define fi first
#define se second
#define db double
#define lowbit(x) ((x)&(-(x)))
const int maxn = 5e5+10;
const int mod = 998244353;
const ll INF = 0x3f3f3f3f;
const db eps = 1e-7;
int n,m;
map<pii,int> mp;
vector<int> G[maxn];
int dis[maxn];
ll ans = 0;
void bfs(int x) {
	for(int i = 1; i <= n; i++) dis[i] = INF; 
	dis[x] = 0; 
	queue<int> q; q.push(x);
	while(!q.empty()) {
		x = q.front(); q.pop();
		ans += dis[x];
//		cerr<<x<<' '<<dis[x]<<'\n';
		for(int to : G[x]) if(dis[to] > dis[x]+1) {
			dis[to] = dis[x] + 1;
			q.push(to);
		}
	}
//	for(int i = 1; i <= n; i++) cerr<<dis[i]<<' '; cerr<<'\n';
}
int siz[maxn];
void dfs(int x,int fa = 0) {
	siz[x] = 1;
	for(int to : G[x]) if(to != fa) {
		dfs(to,x);
		siz[x] += siz[to];
		ans += 1ll * siz[to] * (n-siz[to]);
	}
}
pii a[maxn];
bool cmp(pii x,pii y) {
	return x.se < y.se;
}
void Solve() {
	cin >> n;
	for(int i = 1; i <= n; i++) {
		cin >> a[i].fi >> a[i].se;
		mp[a[i]] = i;
	}
	int ecnt = 0;
	for(int i = 1; i <= n; i++) {
		if(mp.count(MP(a[i].fi,a[i].se+1))) {
			G[i].pb(mp[MP(a[i].fi,a[i].se+1)]);
		}
		if(mp.count(MP(a[i].fi,a[i].se-1))) {
			G[i].pb(mp[MP(a[i].fi,a[i].se-1)]);
		}
		if(mp.count(MP(a[i].fi+1,a[i].se))) {
			G[i].pb(mp[MP(a[i].fi+1,a[i].se)]);
		}
		if(mp.count(MP(a[i].fi-1,a[i].se))) {
			G[i].pb(mp[MP(a[i].fi-1,a[i].se)]);
		}
		ecnt += G[i].size();
	}
	if(n <= 4000) {
		for(int i = 1; i <= n; i++) {
			bfs(i);
		}
		cout<<ans/2<<'\n';
		return ;
	}
//	cerr<<ecnt<<'\n';
	if(ecnt/2 == n-1) {
		dfs(1);
		cout<<ans<<'\n';
		return;
	}
	sort(a+1,a+n+1);
	ll tot = 0,sum = 0;
	for(int i = 1; i <= n; i++) {
		ans += 1ll*(i-1)*a[i].fi-sum; sum += a[i].fi;
	}
	sort(a+1,a+n+1,cmp);
	tot = sum = 0;
	for(int i = 1; i <= n; i++) {
		ans += 1ll*(i-1)*a[i].se-sum; sum += a[i].se;
	}
	cout<<ans<<'\n';
}
int main() {
	freopen("game.in","r",stdin);
	freopen("game.out","w",stdout);
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//	int TT; cin >> TT; while(TT--)
	Solve();
	return 0;
}
